@isTest
private class InstrumentPolicyControllerTest {
    static testMethod void testMethod1() {
        
        Product__c p  = TestUtils.createInst();
        p.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        p.Category0__c  = '仪器';
        insert p ;

      
              // 客户
        Account acc = TestUtils.createAccount();
        acc.Level__c = '一级医院';
        acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Dealer', 'Account');
        insert acc;

        // 业务机会
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        opp.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('DealerOpportunity', 'Opportunity');
        insert opp;

        IntentProduct__c ip = new IntentProduct__c();
        ip.Product__c = p.Id;
        ip.Quantity__c = 1;
        ip.SalesPrice__c = 1;
        ip.Opportunity__c = opp.Id;
        insert ip;

        //1.创建仪器政策模板
    	MarketingPolicy__c  mp = new MarketingPolicy__c();
    	mp.Product__c =p.Id ;
    	mp.Category1__c = '生化';
    	mp.Category2__c = '生化-试剂';
    	mp.AmountPurchased__c = 10000;
    	mp.BloodCardPurchased__c = 12000;
    	mp.Company__c = 'MKSY';
    	mp.CooperationPeriod__c = 5;
    	mp.EarnestMoney__c = 10000;
        mp.PolicyType__c = '租赁合作政策';

    	insert mp ; 

    	//
        PageReference page = new PageReference('/apex/InstrumentPolicy?id=' + opp.Id );
        System.Test.setCurrentPage(page);
    	InstrumentPolicyController ipc = new InstrumentPolicyController();

    	ipc.init();
    	ipc.setNil();

    	ipc.getIntentProduct();
        List<InstrumentPolicyController.PolicyBean> saveList = new List<InstrumentPolicyController.PolicyBean>();
        saveList.add(new InstrumentPolicyController.PolicyBean(ip,null));
        saveList.add(new InstrumentPolicyController.PolicyBean(mp,null));
    	ipc.saveJson = EncodingUtil.urlEncode( JSON.serialize(saveList), 'UTF-8');

    	ipc.save();

    	InstrumentPolicy__c insp = [select Id ,Opportunity__c,Status__c from InstrumentPolicy__c  limit 1 ];
        
        System.debug('InstrumentPolicyControllerTest==>'+ insp);
        PageReference page1 = new PageReference('/apex/InstrumentPolicy?id=' + insp.Id );
        System.Test.setCurrentPage(page1);
        InstrumentPolicyController ipc1 = new InstrumentPolicyController();

    	ipc1.init();
    	ipc1.getDetails();

    	insp.Status__c = '已批准';
    	update insp;


    }	
}